package xyz.liut.banner;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import xyz.liut.liutlibrarys.R;
import xyz.liut.volley.LiutVolley;

public class LiutBanner extends FrameLayout {
    private LinearLayout ll_dot;
    private List<ImageView> iv_dots;
    private List<ImageView> imageViews;
    private ViewPager vp;
    private Context context;
    private int count;
    private int currentItem;
    private boolean isAutoPlay;
    private Handler handler;
    private LiutVolley volley;
    private OnBannerClickListener listener;

    public LiutBanner(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        initData();
    }

    public LiutBanner(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        this.context = context;
        initData();
    }

    public LiutBanner(Context context) {
        this(context, null);
        this.context = context;
        initData();
    }

    private void initData() {
        if (isInEditMode()) {
            return;
        }
        volley = LiutVolley.getInstance(getContext());
        imageViews = new ArrayList<>();
        iv_dots = new ArrayList<>();
        handler = new Handler();
    }

    public void setImgUrlAndClickUrl(List<String> imagesUrl, boolean isVisibilityDot, boolean isFitXY, OnBannerClickListener listener) {
        if (listener != null) {
            this.listener = listener;
        }
        initLayout();
        initImgFromNet(imagesUrl, isVisibilityDot, isFitXY);
        showTime();
    }


    public void setImgResAndClickUrl(List<Integer> imagesRes, boolean isVisibilityDot, boolean isFitXY, OnBannerClickListener listener) {
        if (listener != null) {
            this.listener = listener;
        }
        initLayout();
        initImgFromRes(imagesRes, isVisibilityDot, isFitXY);
        showTime();
    }

    private void initLayout() {
        imageViews.clear();
        View view = LayoutInflater.from(context).inflate(
                R.layout.liut_banner_layout, this, true);
        vp = (ViewPager) view.findViewById(R.id.vp);
        ll_dot = (LinearLayout) view.findViewById(R.id.ll_dot);
        ll_dot.removeAllViews();
    }

    private void initImgFromRes(List<Integer> imagesRes, boolean isVisibilityDot, boolean isFitXY) {
        count = imagesRes.size();
        for (int i = 0; i < count; i++) {
            ImageView ivDot = new ImageView(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            params.leftMargin = 5;
            params.rightMargin = 5;
            ivDot.setImageResource(R.drawable.dot_blur);
            ll_dot.addView(ivDot, params);
            iv_dots.add(ivDot);
            if (!isVisibilityDot) {
                ll_dot.setVisibility(GONE);
            }
        }
        iv_dots.get(0).setImageResource(R.drawable.dot_focus);


        for (int i = 0; i <= count + 1; i++) {
            ImageView iv = new ImageView(context);

            if (isFitXY) {
                iv.setScaleType(ScaleType.FIT_XY);
            }

//            iv.setBackgroundResource(R.drawable.loading);

            if (i == 0) {
                iv.setImageResource(imagesRes.get(count - 1));
            } else if (i == count + 1) {
                iv.setImageResource(imagesRes.get(0));
            } else {
                iv.setImageResource(imagesRes.get(i - 1));
            }
            imageViews.add(iv);
            setOnClickListener(imageViews);
        }
    }

    //监听
    private void setOnClickListener(List<ImageView> imageViews) {
        if (listener == null) {
            return;
        }
        ImageView imageView;
        for (int i = 0; i < imageViews.size(); i++) {
            imageView = imageViews.get(i);
            final int finalI = i;
            imageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onBannerListener(finalI - 1);
                }
            });
        }
    }

    private void initImgFromNet(List<String> imagesUrl, boolean isVisibilityDot, boolean isFitXY) {
        count = imagesUrl.size();
        for (int i = 0; i < count; i++) {
            ImageView iv_dot = new ImageView(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            params.leftMargin = 5;
            params.rightMargin = 5;
            iv_dot.setImageResource(R.drawable.dot_blur);
            ll_dot.addView(iv_dot, params);
            iv_dots.add(iv_dot);
            if (!isVisibilityDot) {
                ll_dot.setVisibility(GONE);
            }
        }
        iv_dots.get(0).setImageResource(R.drawable.dot_focus);

        for (int i = 0; i <= count + 1; i++) {
            ImageView iv = new ImageView(context);
            if (isFitXY) {
                iv.setScaleType(ScaleType.FIT_XY);
            }
//            iv.setBackgroundResource(R.mipmap.loading);
            if (i == 0) {
                volley.getBitmapRequest(imagesUrl.get(count - 1), iv);
            } else if (i == count + 1) {
                volley.getBitmapRequest(imagesUrl.get(0), iv);
            } else {
                volley.getBitmapRequest(imagesUrl.get(i - 1), iv);
            }
            imageViews.add(iv);
            setOnClickListener(imageViews);
        }
    }

    private void showTime() {
        vp.setAdapter(new KannerPagerAdapter());
        vp.setFocusable(true);
        vp.setCurrentItem(1);
        currentItem = 1;
        vp.addOnPageChangeListener(new MyOnPageChangeListener());
        startPlay();
    }

    private void startPlay() {
        isAutoPlay = true;
        handler.postDelayed(task, 2000);
    }

    private final Runnable task = new Runnable() {

        @Override
        public void run() {
            if (isAutoPlay) {
                currentItem = currentItem % (count + 1) + 1;
                if (currentItem == 1) {
                    vp.setCurrentItem(currentItem, false);
                    handler.post(task);
                } else {
                    vp.setCurrentItem(currentItem);
                    handler.postDelayed(task, 3000);
                }
            } else {
                handler.postDelayed(task, 5000);
            }
        }
    };

    class KannerPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return imageViews.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            View view = imageViews.get(position);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(imageViews.get(position));
        }

    }

    class MyOnPageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
            switch (arg0) {
                case 1:
                    isAutoPlay = false;
                    break;
                case 2:
                    isAutoPlay = true;
                    break;
                case 0:
                    if (vp.getCurrentItem() == 0) {
                        vp.setCurrentItem(count, false);
                    } else if (vp.getCurrentItem() == count + 1) {
                        vp.setCurrentItem(1, false);
                    }
                    currentItem = vp.getCurrentItem();
                    isAutoPlay = true;
                    break;
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int arg0) {
            for (int i = 0; i < iv_dots.size(); i++) {
                if (i == arg0 - 1) {
                    iv_dots.get(i).setImageResource(R.drawable.dot_focus);
                } else {
                    iv_dots.get(i).setImageResource(R.drawable.dot_blur);
                }
            }
        }

    }

}
